package org.jeecg.modules.pay.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.boot.starter.lock.annotation.JRepeat;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.pay.entity.AccountChange;
import org.jeecg.modules.pay.service.IAccountChangeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 账户变动记录
 * @Author: jeecg-boot
 * @Date: 2022-06-14
 * @Version: V1.0
 */
@Api(tags = "账户变动记录")
@RestController
@RequestMapping("/com.pay/accountChange")
@Slf4j
public class AccountChangeController extends JeecgController<AccountChange, IAccountChangeService> {
    @Autowired
    private IAccountChangeService accountChangeService;

    /**
     * 分页列表查询
     *
     * @param accountChange
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "账户变动记录-分页列表查询")
    @ApiOperation(value = "账户变动记录-分页列表查询", notes = "账户变动记录-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AccountChange>> queryPageList(AccountChange accountChange,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<AccountChange> queryWrapper = QueryGenerator.initQueryWrapper(accountChange, req.getParameterMap());
        IPage<AccountChange> pageList = accountChangeService.getPage(pageNo, pageSize, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询自己的账户变动
     *
     * @param accountChange
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "账户变动记录-分页列表查询")
    @ApiOperation(value = "账户变动记录-分页列表查询自己的账户变动", notes = "账户变动记录-分页列表查询自己的账户变动")
    @GetMapping(value = "/myList")
    public Result<IPage<AccountChange>> querySelfPageList(AccountChange accountChange,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        IPage<AccountChange> pageList = accountChangeService.getMyRecord(pageNo, pageSize, sysUser.getId());
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param accountChange
     * @return
     */
    @AutoLog(value = "账户变动记录-添加")
    @ApiOperation(value = "账户变动记录-添加", notes = "账户变动记录-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AccountChange accountChange) {
        accountChangeService.add(accountChange);
        return Result.OK("添加成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "账户变动记录-通过id查询")
    @JRepeat(lockKey = "#name", lockTime = 5)
    @ApiOperation(value = "账户变动记录-通过id查询", notes = "账户变动记录-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AccountChange> queryById(@RequestParam(name = "id", required = true) String id) {
        AccountChange accountChange = accountChangeService.get(id);
        if (accountChange == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(accountChange);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param accountChange
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AccountChange accountChange) {
        return super.exportXls(request, accountChange, AccountChange.class, "账户变动记录");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AccountChange.class);
    }

}
